<?php

$date=isset($_GET['date'])? $_GET['date'] : TimeToDate(time());

LoadOnlineHistory($date);


function LoadOnlineHistory($date)
{
global $list,$dbsql;
	$list=array();
	$count=0;
	
	$time1=DateToTime($date);
	$time2=$time1+(60*60*24);
		
	$sql_select=" select * from tblonline where iTime>=$time1 and iTime<=$time2";
	$rs=$dbsql->query($sql_select);
	while($result = $dbsql->fetch_array($rs))
	{			
		if(empty($result['cCountry']))
		{
			$result['cCountry']=Ip2Country($result['cIp']);
			InsertCountry($result['cIp'],$result['cCountry']);
		}
		$list[$count]=$result;
		$count++;
	}
	$list['count']=$count;
	return $list;	
}

function InsertCountry($ip,$country)
{
global $language,$dbsql;
	$sql_update="update tblonline set cCountry='$country' where cIp='$ip'";
	$dbsql->query($sql_update);
}

function Ip2Country($ip)
{
	if(substr($ip,0,3)=="192" || substr($ip,0,3)=="127")
	{
		return "Local";
	}else
	if(substr($ip,0,3)=="123")
	{
		return "VietNam";
	}
	return file_get_contents('http://api.hostip.info/country.php?ip='.$ip);
}

function ListDate()
{
	global $dbsql;
	$sql_select="select * from tblonline order by iTime desc";
	$rs=$dbsql->query($sql_select);
	$listdate=array();
	$count=0;
	$str="";
	while($result=$dbsql->fetch_array($rs))
	{
		$time=TimeToDate($result['iTime']);
	
		if(!in_array($time,$listdate))
		{
			$listdate[$count++]=$time;
			$str.='<option value="'.$time.'">'.$time.'</option>';
		}
	}
	return $str;
}

function DateToTime($str)
{
	$str=explode("-",$str);
	$year=$str[0];
	$mon=$str[1];
	$day=$str[2];	
	
	return  mktime(0, 0, 0, $mon, $day, $year);
}

function TimeToDate($str)
{
	return date("Y-m-d",$str);
}
?>